<?php
	date_default_timezone_set('America/New_York');
	require '../../slimloader/Slim/Slim.php';
	require '../helpers/GeneralUtil.php';
	require '../helpers/Config.php';
	require '../helpers/DbHelper.php';

	\Slim\Slim::registerAutoloader();
	
	$app = new \Slim\Slim(array(
		'debug'=>true
	));
	$app->post('/save',function() use ($config,$app){
		$respData=array();
		$apikey = $app->request->post("devapipassword");
		if($apikey!=$config['devapipassword']){
			echo json_encode(generateError('0','Miss Match API Key'));
			exit;
		}
		$dbHelper =new DbHelper();
		$con = mysqli_connect($config['database']['host'],$config['database']['username'],$config['database']['password'],$config['database']['table']);
		if (mysqli_connect_errno()){
				$respData=generateError('0',"Failed to connect to MySQL: " . mysqli_connect_error());
		}else{
			$phoneNumber = $app->request->post("phoneNumber");
			$password= $app->request->post("password");
			$salt = $dbHelper->getSalt($con);
			$pwd=crypt($password,$salt);
			$success = $dbHelper->saveUser($phoneNumber,$pwd,$con);
			if($success){
				$respData=generateError("1","Thank you for registering with Worktive");
				$respData['user']=array('phoneNumber'=>$phoneNumber,'token'=>$pwd);
			}else{
				$respData=generateError("0","issue saving current user");
				
			}
		}
		echo json_encode($respData);
	});
	$app->post('/auth',function() use ($config,$app){
		$respData=array();
		$apikey = $app->request->post("devapipassword");
		if($apikey!=$config['devapipassword']){
			echo json_encode(generateError('0','Miss Match API Key'));
			exit;
		}
		$dbHelper =new DbHelper();
		$con = mysqli_connect($config['database']['host'],$config['database']['username'],$config['database']['password'],$config['database']['table']);
		if (mysqli_connect_errno()){
				$respData=generateError('0',"Failed to connect to MySQL: " . mysqli_connect_error());
		}else{
			$phoneNumber = $app->request->post("phoneNumber");
			$password= $app->request->post("password");
			
			$enPwd="";
			$unsalted=$app->request->post("unsalted");
			$unsalted=$unsalted==null || $unsalted==""?"1":"0";
			if($unsalted=="1"){
				$salt = $dbHelper->getSalt($con);
				$enPwd = crypt($password,$salt);
			}else{
				$enPwd = $password;
			}
			$success = $dbHelper->authUser($phoneNumber,$enPwd,$con);
			if($success){
                                $isActive=false;
                                while($row = mysqli_fetch_array($success)){
                                    $active= $row['isActive'];
                                    $isActive = $active==='1'?true:false;
                                    break;
                                }
                                if($isActive){
                                    $respData=generateError("1","Welcome to Worktive ".$phoneNumber);
                                    $respData['user']=array('phoneNumber'=>$phoneNumber,'token'=>$enPwd);
                                }else{
                                    $respData=generateError("2","Verification Code needed");
                                }
			}else{
				$respData=generateError("0","issue authenticate user");
			}
		}
		echo json_encode($respData);
	});
	$app->run();
	
?>